/***************** Modification History ***********************************
   @doc
   @module comm.c - Functions for communication |
   @end

$Log: /SoftPay/SPK2SIM/Source/Comm/comm.c $
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#define VSCOMM_C

#include <stdio.h>
#include <acldev.h>
#include <aclutil.h>
#include <xmodem.h>
#include <string.h>
#include <txoinc.h>
#include <aclconio.h>


#include <project.h>
/** /#define DEBUG_VSCOMM*/
#ifdef DEBUG_VSCOMM
#define DEBUG_95
#endif

#include <define.h>
#include <modem.h>
#include <comm.h>
#include <spdebug.h>
#include <tct.h>
#include <confio.h>
#include <funcids.h>
#include <msg.h>
#include <msgfuncs.h>
#include <mdlfuncs.h>
#include <operate.h>
#include <ui.h>
#include <modemmsg.h>
#include <isdn.h>
#include <dp3201.h>
#include <dp3101.h>
#include <mbtx2.h>
#include <spdmodem.h>
#include <codesec.h>
#include <gds.h>
#include <logsys.h>

COMM_OBJECT obCommunication;
#ifdef TCPIP_DEFINED
extern int vdInitializeTCPIPComm(COMM_OBJECT* obCommunication);
#endif /* TCPIP_DEFINED */

#ifdef CSCLNT_DEFINED
//jrd 01052004
extern void vdInitializeCSCLNTComm (COMM_OBJECT *obCommunication);
#endif /* CSCLNT_DEFINED */

extern void vdInitializeUclComm (COMM_OBJECT *obCommunication);

//extern void vdInitializeUCLGPRSComm(COMM_OBJECT *obCommunication);
//extern void vdInitializeUCLComm(COMM_OBJECT *obCommunication); //KiaSoon //LIEM DISABLE

int inInitCOMM_OBJECT(TRANSACTION_OBJECT* pobTran) {
    MEMCLR(&obCommunication, sizeof(COMM_OBJECT));
    vdDisplayMessageFromFile(MODEM_CHECK_MSG21, STATUS_MSG);
    if ( inInitializeComm (MODEM_COMM) != VS_SUCCESS )
    {
    	//To make Comm Err more descriptive.....Renu
    	 //vdSGErrorMessage(TXMT_FAIL_ERR_MSG);
    	 vdSGErrorMessage(TXMT_FAIL_ERR_0001_MSG);
	ACTIVITY_LOG("InitializeComm Failed: 0001");
        return (VS_ERR);
    }

    return (VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    Function : inInitializeComm()
    Description :
    Parameters :
    Returns :
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   07/02/97 Kevin_v1   Creation.
      2   09/21/98 Monique_B1 Added case for 3201B & 3201T
      3   09/24/98 Monique_B1 Added case for 3101, Reduce 3201 to one case
--------------------------------------------------------------------------*/

int inInitializeComm(int inCommLinkType) {
    int inRetVal = VS_SUCCESS;
    TRANSACTION_OBJECT* pobTran;

    pobTran = pstGet_pobTran();
    if (pobTran == NULL)
        return VS_ERR;

LOG_PRINTFF((0x08L, "inInitializeComm %d ==%d  ",obCommunication.inLinkType, inCommLinkType));
    pdebug(("--inInitComm(%d/%d)--", obCommunication.inLinkType, inCommLinkType));
    /*
     *      Unload current COMM Object and load new if required.
     */
     
    if (obCommunication.inLinkType != inCommLinkType) {
        /* check if there's no comm object before trying to Deinitialize it */
        if (obCommunication.inLinkType != NO_COMM) // LAC1.4 change
        {
            if (obCommunication.inDeinitialize) {
                pdebug(("  DeInit"));
                obCommunication.inDeinitialize(&obCommunication);
            }
        }
    	

 vdInitializeUclComm(&obCommunication);
  inRetVal = obCommunication.inInitialize(&obCommunication);
    	}
  return inRetVal;
     
   
	#if 0 	
	#ifdef IMAM_ETHERNET
	//	inCommLinkType=CONNECT_ONE;
		#endif
        switch (inCommLinkType) {
        case MODEM_COMM:
            pdebug(("  Init Modem"));
            vdInitializeModemComm(&obCommunication);
            break;

#ifdef ISDN_DEFINED
        case ISDN_COMM:
            pdebug(("  Init ISDN"));
            vdInitializeISDNComm(&obCommunication);
            break;
#endif

#ifdef SPDDIAL_DEFINED

        case SPD_MODEM_COMM:
            pdebug(("  Init Speed Dial Modem"));
            vdInitializeSpdModemComm(&obCommunication);
            break;

#endif

case COMM_3101:
            pdebug(("  Init 3201 "));
           // vdInitialize3201Comm(&obCommunication);
           LOG_PRINTFF((0x08L, "inInitializeComm %d ==%d  ",obCommunication.inLinkType, inCommLinkType));

            pdebug(("  Init Connect One "));
     //   vdInitializeTCPIPComm(&obCommunication);
     //   inInitUclCommunication(&obCommunication);
	 vdInitializeUclComm(&obCommunication);
           
            break;
			
#ifdef DP3201_DEFINED
        case COMM_3201:
            pdebug(("  Init 3201 "));
            vdInitialize3201Comm(&obCommunication);
            break;
#endif

#ifdef DP3101_DEFINED
        case COMM_3101:
            pdebug(("  Init 3101 "));
            vdInitialize3101Comm(&obCommunication);
            break;
#endif

#ifdef MOBITEX_DEFINED
        case MOBITEX_COMM:
            pdebug(("  Init Mobitex "));
            vdInitializeMobitexComm(&obCommunication);
            break;
#endif

#ifdef TCPIP_DEFINED

        case CONNECT_ONE :
		LOG_PRINTFF((0x08L, "inInitializeComm %d ==%d  ",obCommunication.inLinkType, inCommLinkType));

            pdebug(("  Init Connect One "));
        //    vdInitializeTCPIPComm(&obCommunication);
        //inInitUclCommunication(&obCommunication);
            break;
#else /*TCPIP_DEFINED */

	case CONNECT_ONE:
	//	vdInitializeUCLComm(&obCommunication);  // DISABLE IT
		
	break;
#endif

#ifdef VCS_DEFINED
	   case CSCLNT_COMM:
		    pdebug(("  Init Connect One "));
			vdInitializeCSCLNTComm(&obCommunication);
			break;
#endif /*VCS_DEFINED */



        default:
        
            if (inRunFunction(pobTran, inCommLinkType) == VS_ERR)
                return(VS_ERR);
            break;
        }

        inRetVal = obCommunication.inInitialize(&obCommunication);
    }

    return (inRetVal);
	#endif
}

/*-------------------------------------------------------------------------
    Function :      inTimeout_TICKS_To_10MS ( int )
    Description :   Timeout conversion, converts timeouts from 10ms Units to 1s Units
                    for modem routine.
    From VSMODEM.C originally...
    "This value is divided by 1.5 to compensate for what appears to be an
    error in the routine mdm_receive_data . Monique_B1 09/20/96
    inTimeout = (inReceiveTimeout * 100) / 1.5"
    Parameters :
    Returns :      VS_SUCCESS, VS_ERR
    Globals :      MODEM_OBJECT, obcommunication
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   07/11/97  Kevin_v1               Creation
--------------------------------------------------------------------------*/

int inTimeout_TICKS_To_10MS(int inTicks) {
    int inRetVal;

    inRetVal = (int) (inTicks / A_SECOND);
    inRetVal *= 100;
    inRetVal = (int) (inRetVal / 1.5);

    pdebug(("10MS=%d/%d", inTicks, inRetVal));
    return (inRetVal);
} /* End of inTimeout_TICKS_To_10MS () */

/*-------------------------------------------------------------------------
    Function :      inTimeout_SEC_To_TICKS ( int )
    Description :   Timeout conversion, converts timeouts from to 1s Units
                    to Clock Tick Units.
    Parameters :
    Returns :      VS_SUCCESS, VS_ERR
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   07/22/97  Kevin_v1               Creation
--------------------------------------------------------------------------*/


int inTimeout_SEC_To_TICKS(int inSec) {
    int inRetVal;

    inRetVal = (int) (inSec * A_SECOND);

    return (inRetVal);
} /* End of inTimeout_MS_To_SEC () */


/*-------------------------------------------------------------------------
    Function :      inGetCommDevicePath ( int, char * )
    Description :   Returns path to COMM Device.
    Parameters :
    Returns :      VS_SUCCESS, VS_ERR
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   07/30/97  Kevin_v1               Creation
      2   09/21/98 Monique_B1  Added case for port 4
--------------------------------------------------------------------------*/

int inGetCommDevicePath(char chPort,char* szPath) {
    long lnMessageIndex;

    /* select com port */
    /*    switch ( (char) inPort )*/
    switch (chPort) {
    case '1':
        lnMessageIndex = MSG_DEV_COM1;
        break;
    case '2':
        lnMessageIndex = MSG_DEV_COM2;
        break;
    case '3':
        lnMessageIndex = MSG_DEV_COM3;
        break;
    case '4':
        lnMessageIndex = MSG_DEV_COM4;
        break;
    case '5':
    case 'P':
    case 'p':
        lnMessageIndex = MSG_DEV_PINPAD;
        break;
    default:
        return VS_ERR;
    }   /* end switch */

    vdGetMessageFromFile(lnMessageIndex, szPath);
    return VS_SUCCESS;
}

/*
 * get interchar timeout
 * as needed
 */
void vdGetIcto(int* pinIcto) {
    MODEM_OBJECT* obModem = (MODEM_OBJECT*) 0;
    ISDN_OBJECT* obISDN = (ISDN_OBJECT*) 0;

    if (pinIcto) {
        if (fISDN()) {
            obISDN = (ISDN_OBJECT *) obCommunication.Data;
            (*pinIcto) = (int) T3POS_INTER_CHAR_TO;
        } else {
            obModem = (MODEM_OBJECT *) obCommunication.Data;
            (*pinIcto) = obModem->shGetInterCharTO(obModem->srCPT);
        }
    }

    return;
}


int inDefaultCustomCommInit(TRANSACTION_OBJECT* pobTran) {
    vdDisplayErrorString("NO CUSTOM INIT EXISTS");
    return(VS_ERR);
}


int COMM_CODE_CHECK(char* a,char* b,short * c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);
    return 1;
}


#undef VSCOMM_C

